package com.beijiteshop.shop.wxapi

import android.content.Intent
import android.os.Bundle
import com.beijiteshop.libcommon.base.activity.BaseActivity
import com.beijiteshop.libcommon.utils.EventUtil
import com.beijiteshop.shop.R
import com.beijiteshop.shop.databinding.ActivityWxEntryBinding
import com.beijiteshop.shop.model.event.EventKey
import com.beijiteshop.shop.ui.login.vm.UserVM
import com.beijiteshop.shop.ui.main.MainActivity
import com.blankj.utilcode.util.ActivityUtils
import com.tencent.mm.opensdk.modelbase.BaseReq
import com.tencent.mm.opensdk.modelbase.BaseResp
import com.tencent.mm.opensdk.modelmsg.SendAuth
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler
import org.koin.android.viewmodel.ext.android.viewModel
import java.lang.Exception

/**
 * @Author LiaoZhongKai
 * @Date 2021/3/24 17:52
 * @Description 微信授权页面
 */
class WXEntryActivity : BaseActivity<ActivityWxEntryBinding>(), IWXAPIEventHandler {

    private val mUserVM: UserVM by viewModel()

    override fun getLayoutRes(): Int = R.layout.activity_wx_entry

    override fun initView(savedInstanceState: Bundle?) {
        WXHelper.getInstance().handleIntent(intent,this)
    }

    override fun initData() {
    }

    override fun initEvent() {
        startObserve()
    }

    override fun onResp(p0: BaseResp?) {
        p0?.let { resp ->
            if (resp.errCode == BaseResp.ErrCode.ERR_OK){
                try {
                    resp as SendAuth.Resp
                    when(resp.state){
                        //登录
                        WXHelper.TYPE_LOGIN -> {
                            login(resp.code)
                        }
                        //获取授权
                        WXHelper.TYPE_AUTH -> {
                            getAuth(resp.code)
                        }
                    }

                }catch (e: Exception){
                    finish()
                }
            }else{
                showToast(resp.errStr)
                finish()
            }
        }
    }

    override fun onReq(p0: BaseReq?) {
    }

    private fun login(code: String){
        showLoading(resources.getString(R.string.logging))
        mUserVM.wxLogin(code)
    }

    private fun getAuth(code: String){
        showLoading(resources.getString(R.string.binding))
        mUserVM.bindWeChat(code)
    }

    private fun startObserve(){
        mUserVM.liveWXLogin.observeKt { result ->
            hideLoading()
            result?.let {
                startActivity(Intent(this,MainActivity::class.java))
                ActivityUtils.finishOtherActivities(MainActivity::class.java)
            }
        }

        mUserVM.liveBindWX.observeKt {
            hideLoading()
            it?.let {result ->
                if (result){
                    showToast(resources.getString(R.string.bind_success))
                    EventUtil.post(EventKey.ACCOUNT_BIND_SUCCESS)
                    finish()
                }
            }
        }

        mUserVM.liveNetError.observeKt {
            hideLoading()
            showToast(it?:resources.getString(R.string.something_error))
            finish()
        }

        mUserVM.liveError.observeKt {
            hideLoading()
            showToast(it?:resources.getString(R.string.something_error))
            finish()
        }
    }
}